
<?php
if(isset($_POST["sub"])){
	//导入数据库配置文件
	require("../../conn/conn.php");
	$id = $_POST["id"];
	$newpwd = $_POST["new_pwd"];
	$repwd = $_POST["new_pwd2"];
	
	//判断用户名格式是否正确
	if(strlen($id) != 11){
		echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."请输入正确的用户名格式（11位）！"."\"".")".";"."</script>";
		echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."../html/忘记密码.html"."\""."</script>";
		exit;
	}else{
		//sql语句
		$sql_select_par = "select * from par_info where par_id = '$id'";
		$sql_select_stu = "select * from stu_info where stu_id = '$id'";
		//获取两种结果
		$ret_par = mysqli_query($conn,$sql_select_par);
		$ret_stu = mysqli_query($conn,$sql_select_stu);
		$row_par = mysqli_fetch_array($ret_par);
		$row_stu = mysqli_fetch_array($ret_stu);
		//判断用户是否存在家长数据库中
		if($id == $row_par['par_id']){
			//判断是否有空输入
			if($newpwd=='' || $repwd==''){
				echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."密码不能为空，请重新输入！"."\"".")".";"."</script>";
				echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."../html/忘记密码.html"."\""."</script>";
				exit;
			}else{
				//判断两次输入的密码是否相同
				if($newpwd == $repwd){
					//sql语句
					require("../../conn/conn.php");
					$sql_update = "update par_info set par_pwd = '$newpwd' where par_id = '$id'";
					mysqli_query($conn,$sql_update);
					echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."密码修改成功，请重新登录！"."\"".")".";"."</script>";
					echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."../html/家长登录页.html"."\""."</script>";
					exit;
				}else{
					echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."两次输入的密码不同，请重新输入！"."\"".")".";"."</script>";
					echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."../html/忘记密码.html"."\""."</script>";
					exit;
				}
			}
		}
		//判断用户是否存在大学生数据库中
		else if($id == $row_stu['stu_id']){
			if($newpwd=='' || $repwd==''){
				echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."密码不能为空，请重新输入！"."\"".")".";"."</script>";
				echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."../html/忘记密码.html"."\""."</script>";
				exit;
			}else{
				//判断两次输入的密码是否相同
				if($newpwd == $repwd){
					//sql语句
					require("../../conn/conn.php");
					$sql_update = "update stu_info set stu_pwd = '$newpwd' where stu_id = '$id'";
					mysqli_query($conn,$sql_update);
					echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."密码修改成功，请重新登录！"."\"".")".";"."</script>";
					echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."../html/大学生登录页.html"."\""."</script>";
					exit;
				}else{
					echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."两次输入的密码不同，请重新输入！"."\"".")".";"."</script>";
					echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."../html/忘记密码.html"."\""."</script>";
					exit;
				}
			}
		}else{
			echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."用户不存在"."\"".")".";"."</script>";
			echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."../html/忘记密码.html"."\""."</script>";
			exit;
		}
	}
}